package com.lovo.Atm.dao.impl;

import com.lovo.Atm.dao.BaseDAO;
import com.lovo.Atm.dao.UserDAO;
import com.lovo.Atm.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class UserDAOImpl extends BaseDAO implements UserDAO {

    @Override
    public UserDO getByNameAndPassword(String name, String password) {
        this.getConnection();
        statement = null;
        res = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name= ? and u_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            statement.setObject(2, password);
            res = statement.executeQuery();
            if (res.next()) {
                user = new UserDO();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }


    @Override
    public UserDO geById(int id) {
        con = getConnection();
        statement = null;
        res = null;
        UserDO user = null;
        String sql = "select *from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            res = statement.executeQuery();
            if (res.next()) {
                user = new UserDO();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String name) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet res = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name= ?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, name);
            res = statement.executeQuery();
            if (res.next()) {
                user = new UserDO();
                user.setId(res.getInt("u_id"));
                user.setName(res.getString("u_name"));
                user.setPassword(res.getString("u_password"));
                user.setBlance(res.getDouble("u_blance"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, res);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        this.getConnection();
        String sql = "update t_user set u_password=?,u_blance=? where u_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getPassword());
            statement.setObject(2, user.getBlance());
            statement.setObject(3, user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close(con, statement);
        }
    }
}
